﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Audio;
using TiledLib;
using Microsoft.Xna.Framework.Media;

namespace MvdDonk.Bomberman.Managers
{
    class ResourceManager
    {
        private Dictionary<string, Texture2D> Textures;
        private Dictionary<string, SoundEffect> SoundEffects;
        private Dictionary<string, Song> Songs;
        private Dictionary<string, Map> Maps;
        private Dictionary<string, SpriteFont> SpriteFonts;

        public ResourceManager()
        {
            Textures = new Dictionary<string, Texture2D>();
            Maps = new Dictionary<string, Map>();
            SpriteFonts = new Dictionary<string, SpriteFont>();
        }

        public Texture2D RequestTexture(string name)
        {
            return GetResource<Texture2D>(name, Textures);
        }

        public Map RequestMap(string name)
        {
            return GetResource<Map>(name, Maps);
        }

        public SpriteFont RequestFont(string name)
        {
            return GetResource<SpriteFont>(name, SpriteFonts);
        }

        public Song RequestSong(string name)
        {
            return GetResource<Song>(name, Songs);
        }

        public SoundEffect RequestSoundEffect(string name)
        {
            return GetResource<SoundEffect>(name, SoundEffects);
        }

        private T GetResource<T>(string name, Dictionary<string, T> target)
        {
            if (name == null)
                return default(T);

            if (target.ContainsKey(name))
                return (T)target[name];
            else
            {
                var temp = Bomberman.Instance.Content.Load<T>(name);
                target.Add(name, temp);
                return temp;
            }
        }
    }
}
